<template>
  <div class="app-container">
    <header-title/>
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="110px"
  >
      <el-form-item label="关键词" prop="keyword">
          <el-input
              v-model="queryParams.keyword"
              placeholder="请输入关键词"
              clearable
              @keyup.enter.native="handleQuery"
          />
      </el-form-item>
      <el-form-item class="mar-r-20">
        <el-button type="primary" icon="el-icon-search" @click="handleQuery"
        >搜索</el-button
        >
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        <el-button  type="primary" icon="el-icon-plus" @click="onHandleAdd">新增协议</el-button>
       
      </el-form-item>
    </el-form>
    <el-table
      v-loading="loading"
      :data="tableList"
      ref="singleTable"
    >   
        <el-table-column label="ID " prop="id" width="150"></el-table-column>
        <el-table-column label="协议类型 " >
          <template slot-scope="scope">
             {{selectDictLabel(dict.type.agreement_type,scope.row.type) || '-'}}
          </template>
        </el-table-column>
        <!-- <el-table-column label="协议内容"   min-width="300" show-overflow-tooltip>
          <template slot-scope="scope">
            <div v-html="scope.row.content"></div>
          </template>
        </el-table-column> -->
        <el-table-column label="创建人" prop="createBy">
          <template slot-scope="scope">
              {{ scope.row.createBy || '-' }}
          </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="createTime">
          <template slot-scope="scope">
              {{ scope.row.createTime || '-' }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="170">
          <template slot-scope="scope">
              <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-search"
                  @click="handleUpdateDetail(scope.row)"
              >详情</el-button>
              <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
              >编辑</el-button>
              <el-button
               size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                >删除</el-button>
            </template>
          </el-table-column>
      </el-table>
      <pagination
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
    >
    </pagination>
    <!-- <el-tabs v-model="form.type" type="card" @tab-click="getAgreement">
      <el-tab-pane :label="item.label" :name="item.value.toString()" v-for="(item,index) in dict.type.agreement_type" :key="index"></el-tab-pane>
    </el-tabs>
    <div>
      <editor v-model="form.content" :min-height="400"/>
    </div>
    <div class="drawer-footer">
      <el-button type="primary" @click="submitForm" :loading="loading">保 存</el-button>
      <el-button @click="cancel">清 空</el-button>
    </div> -->
     <el-dialog
        :title="title"
        :visible.sync="show"
        width="70%"
        append-to-body
        >
      <el-form 
        ref="form"
        :model="form"
        :rules="rules"
        :disabled="type == 'detail' ? true : false"
        label-width="85px">
        <el-row :gutter="20">
            <el-col :span="24">
                <el-form-item label="协议类型" prop="type">
                  <el-select v-model="form.type" clearable >
                    <el-option v-for="dict in dict.type.agreement_type" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
                  </el-select>
                </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="协议内容" prop="content">
                <editor v-model="form.content" :min-height="400"/>
              </el-form-item>
            </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="type != 'detail'">
        <el-button type="primary" @click="onHandleSubmit">确 定</el-button>
        <el-button @click="show = false">取 消</el-button>
    </div>
    </el-dialog>
  </div>
</template>

<script>
import { getAgreement, addAgreement,getAgreementList,delAgreement } from "@/api/system/agreement";

export default {
  name: "Agreement",
  dicts: ['agreement_type'],
  data() {
    return {
      // 遮罩层
      loading: false,
      form:{
        type: '1',
        content: undefined
      },
      queryParams: {
        pageSize: 10,
        pageNum : 1,
        type:''
      },
      total: 0,
      loading: false,
      tableList: [],
      showSearch: true,
      show: false,
      form: {

      },
      title: '',
      type : '',
      rules : {
        type : [
           { required: true, message: "协议类型不能为空", trigger: "change" }
        ],
        content : [
           { required: true, message: "协议内容不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    // this.getAgreement()
    this.getList()
  },
  methods: {
    handleDelete(row) {
     
      this.$modal.confirm('是否确认协议编号为"' + row.id + '"的数据项？').then(function() {
          return delAgreement({id: row.id});
      }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    onHandleSubmit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          addAgreement(this.form).then(res => {
              this.$modal.msgSuccess(res.msg);
              this.show = false;
              this.getList();
            })
          
        }
      })
    },
    beforeClose() {
      this.show = false
    },
    onHandleAdd() {
      this.form = {
        type: '',
        content:''
      }
      this.title = '新增协议'
      this.type = 'add'
      this.show = true
    },
    handleUpdateDetail(row) {
      this.title = '协议详情'
      this.type = 'detail'
      this.show = true
      this.form = row
    },
    handleUpdate(row) {
      this.title = '修改协议'
      this.type = 'edit'
      this.show = true
      this.form = row
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    getList() {
      this.loading = true
      getAgreementList(this.queryParams).then(res => {
        this.loading = false
        this.tableList = res.data.records
        this.total = res.data.total
      })
    },
    /** 查询列表 */
    getAgreement() {
      getAgreement({type:this.form.type}).then(response => {
        this.form = {
          id: response.data?response.data.id:undefined,
          type: response.data?response.data.type.toString():undefined,
          content: response.data?response.data.content:undefined
        };
      });
    },
    cancel(){
      this.form.content = undefined
    },
    /** 提交按钮 */
    submitForm: function() {
      this.loading = true
      addAgreement(this.form).then(response => {
        this.$modal.msgSuccess("保存成功");
        this.loading = false
      }).catch(err=>{
        this.loading = false
      });
    }
  }
};
</script>
